using UnityEngine;

public class HitCollider : MonoBehaviour
{

    public float raycastDistance = Mathf.Infinity;

    // Update is called once per frame

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //new Ray(transform.position, transform.forward); // 从当前物体出发，沿着其正前方发射一条射线  
            RaycastHit[] hits = new RaycastHit[Physics.RaycastAll(ray, raycastDistance).Length];
            // 使用Physics.RaycastAll获取所有被射线击中的物体，并存储在hits数组中  

            for (int i = 0; i < hits.Length; i++)
            {
                hits[i] = Physics.RaycastAll(ray, raycastDistance)[i]; // 将射线检测的结果赋值给hits数组  
                Debug.Log("Hit " + i + " object: " + hits[i].collider.gameObject.name); // 输出被击中的物体名称  
            }
        }
        
    }
}